package cn.edu.sgu.www.mhxysy.service.chongwu_equipment.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.dto.chongwu_equipment.ChongwuEquipmentViewAttributeDTO;
import cn.edu.sgu.www.mhxysy.entity.chongwu_equipment.ChongwuEquipmentCategory;
import cn.edu.sgu.www.mhxysy.entity.chongwu_equipment.ChongwuEquipmentViewAttribute;
import cn.edu.sgu.www.mhxysy.entity.except.Property;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.chongwu_equipment.ChongwuEquipmentCategoryMapper;
import cn.edu.sgu.www.mhxysy.mapper.chongwu_equipment.ChongwuEquipmentViewAttributeMapper;
import cn.edu.sgu.www.mhxysy.mapper.except.PropertyMapper;
import cn.edu.sgu.www.mhxysy.pager.chongwu_equipment.ChongwuEquipmentViewAttributePager;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.chongwu_equipment.ChongwuEquipmentViewAttributeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class ChongwuEquipmentViewAttributeServiceImpl implements ChongwuEquipmentViewAttributeService {

    private final PropertyMapper propertyMapper;
    private final ChongwuEquipmentCategoryMapper chongwuEquipmentCategoryMapper;
    private final ChongwuEquipmentViewAttributeMapper chongwuEquipmentViewAttributeMapper;

    @Autowired
    public ChongwuEquipmentViewAttributeServiceImpl(
            PropertyMapper propertyMapper,
            ChongwuEquipmentCategoryMapper chongwuEquipmentCategoryMapper,
            ChongwuEquipmentViewAttributeMapper chongwuEquipmentViewAttributeMapper) {
        this.propertyMapper = propertyMapper;
        this.chongwuEquipmentCategoryMapper = chongwuEquipmentCategoryMapper;
        this.chongwuEquipmentViewAttributeMapper = chongwuEquipmentViewAttributeMapper;
    }

    @Override
    public void insert(ChongwuEquipmentViewAttributeDTO viewAttributeDTO) {
        viewAttributeDTO.setId(null);

        ChongwuEquipmentViewAttribute viewAttribute = toEntity(viewAttributeDTO);

        chongwuEquipmentViewAttributeMapper.insert(viewAttribute);
    }

    @Override
    public void deleteById(Integer id) {
        chongwuEquipmentViewAttributeMapper.deleteById(id);
    }

    @Override
    public void updateById(ChongwuEquipmentViewAttributeDTO viewAttributeDTO) {
        if (viewAttributeDTO.getId() == null) {
            throw new GlobalException(ResponseCode.BAD_REQUEST, "ID不能为空");
        }

        ChongwuEquipmentViewAttribute viewAttribute = toEntity(viewAttributeDTO);

        chongwuEquipmentViewAttributeMapper.updateById(viewAttribute);
    }

    @Override
    public List<Property> selectByEquipmentId(Integer equipmentId) {
        if (equipmentId != null) {
            ChongwuEquipmentCategory chongwuEquipmentCategory = chongwuEquipmentCategoryMapper.selectById(equipmentId);

            return chongwuEquipmentViewAttributeMapper.selectByEquipmentId(chongwuEquipmentCategory.getType());
        }

        return propertyMapper.selectList(null);
    }

    @Override
    public Page<ChongwuEquipmentViewAttribute> selectByPage(ChongwuEquipmentViewAttributePager pager) {
        QueryWrapper<ChongwuEquipmentViewAttribute> queryWrapper = new QueryWrapper<>();
        Page<ChongwuEquipmentViewAttribute> page = Pager.ofPage(pager);

        queryWrapper.eq(
                pager.getGrade() != null,
                "grade", pager.getGrade()
        );
        queryWrapper.eq(
                pager.getEquipmentId() != null,
                "equipment_id", pager.getEquipmentId()
        );

        return chongwuEquipmentViewAttributeMapper.selectPage(page, queryWrapper);
    }

    private ChongwuEquipmentViewAttribute toEntity(ChongwuEquipmentViewAttributeDTO viewAttributeDTO) {
        // 验证装备属性是否重复
        QueryWrapper<ChongwuEquipmentViewAttribute> queryWrapper = new QueryWrapper<>();

        queryWrapper.ne(
                viewAttributeDTO.getId() != null,
                "id", viewAttributeDTO.getId()
        );
        queryWrapper.eq("grade", viewAttributeDTO.getGrade());
        queryWrapper.eq("property_id", viewAttributeDTO.getPropertyId());
        queryWrapper.eq("equipment_id", viewAttributeDTO.getEquipmentId());

        Long count = chongwuEquipmentViewAttributeMapper.selectCount(queryWrapper);

        if (count > 0) {
            throw new GlobalException(ResponseCode.CONFLICT, "装备属性重复。");
        }

        ChongwuEquipmentViewAttribute viewAttribute = new ChongwuEquipmentViewAttribute();

        viewAttribute.setId(viewAttributeDTO.getId());
        viewAttribute.setMax(viewAttributeDTO.getMax());
        viewAttribute.setMin(viewAttributeDTO.getMin());
        viewAttribute.setGrade(viewAttributeDTO.getGrade());
        viewAttribute.setPropertyId(viewAttributeDTO.getPropertyId());
        viewAttribute.setEquipmentId(viewAttributeDTO.getEquipmentId());

        return viewAttribute;
    }

}